package com.zyl.service.Impl;

import com.zyl.Mapper.UserMapper;
import com.zyl.dto.UserCommentDTO;
import com.zyl.pojo.User;
import com.zyl.service.UserService;
import com.zyl.util.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Zyl
 * @Param:
 * @return:
 * @Description:
 */
@Service
public class UserServiceImpl  implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User queryUserByUsername(String username) {
        return userMapper.queryUserByUsername(username);
    }

    @Override
    public List<User> queryAllUser(Integer page) {
        return userMapper.queryAllUser(page);
    }

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUser(Integer id) {
            userMapper.deleteUser(id);
    }

    @Override
    public User queryUserById(Integer id) {
        return userMapper.queryUserById(id);
    }

    @Override
    public void updatePasswordById(User user) {
        userMapper.updatePasswordById(user);
    }

    @Override
    public Integer findUsername(String username) {
        Integer username1 = userMapper.findUsername(username);
        return username1;
    }

    @Override
    public Integer findEmail(String email) {
        Integer email1 = userMapper.findEmail(email);
        return email1;
    }

    @Override
    public void updateUserPictureById(User user) {
        userMapper.updateUserPictureById(user);
    }

    @Override
    public String findUserPictureById(Integer id) {
        return userMapper.findUserPictureById(id);
    }

    @Override
    public List<UserCommentDTO> UserCommentByResponse(Integer respondentId) {
        return userMapper.UserCommentByResponse(respondentId);
    }

    @Override
    public Integer UserCommentCount(Integer respondentId) {
        return userMapper.UserCommentCount(respondentId);
    }

    @Override
    public void updateIsRead(Integer id) {
        userMapper.updateIsRead(id);
    }

    @Override
    public Integer isReadCount(Integer respondentId) {
        return userMapper.isReadCount(respondentId);
    }

    public boolean doRegister(User user){
        System.out.println("我进来了");
        // 这里可以验证各字段是否为空

        //利用正则表达式（可改进）验证邮箱是否符合邮箱的格式
        if(!user.getEmail().matches("^\\w+@(\\w+\\.)+\\w+$")){
            return false;
        }
        //生成激活码
        String code="123456";
        user.setCode(code);
        user.setState(0);
        //将用户保存到数据库
        //保存成功则通过线程的方式给用户发送一封邮件
        System.out.println(user);
        userMapper.addUser(user);
        int i=0;
        new Thread(new MailUtil(user.getEmail(), code)).start();;
        return true;
    }
}
